import axios from "axios";
import store from "@/store";
console.log(store, 1111);

axios.interceptors.request.use((config) => {
  // console.log(config, '请求拦截');
  return config;
});
axios.interceptors.response.use((res) => {
  console.log(res, "响应拦截");
  if (res.data.code == 401) {
    if (!store.state.lock) {
      store.state.responseConfig = [];
      store.state.lock = true;
      login();
    }
    store.state.responseConfig.push(res.config);
  }
  //登录过期了,就显示弹窗进行登录, 接口是退出登录的话就不需要弹窗了
  //非登录页的时候才显示弹窗
  return res;
});
export default function request(url, data = {}, method = "post") {
  return new Promise((resolve, reject) => {
    const options = {
      url,
      method,
    };
    if (method.toLowerCase() === "get") {
      options.params = data;
    } else {
      options.data = data;
    }
    axios(options)
      .then((res) => {
        // console.log('返回数据', res);
        resolve(res.data);
      })
      .catch((error) => {
        reject(error);
        // console.log('错误', error);
      });
  });
}
